<% 
    @appmodules = AppModule.find(:all, :conditions => [ "usage_type ='F'"])
    @data_access_actions = $data_access_actions
    

%>
<script language='JavaScript' type='text/javascript'>
function checkCollection(daf_type_select_name, divIdName)
{
    var sectypeidx = eval("document.data_access_matrix_form."+daf_type_select_name+".selectedIndex");
    var sectype = eval("document.data_access_matrix_form."+daf_type_select_name+".options[sectypeidx].value");
    //alert(sectype);
    if(sectype=='4')
    {
         showDiv(divIdName);
    }
    else
    {
        hideDiv(divIdName);
    }
    
    
}

function assignListVals(all, asgnd)
{
     var arrall = new Array();
     var arrasgnd = new Array();
     var arrLookup = new Array();
     var i;
     for(i=0; i<asgnd.options.length; i++)
     {
          arrLookup[asgnd.options[i].text] = asgnd.options[i].value;
          arrasgnd[i] = asgnd.options[i].text;
     }
     var fLength = 0;
     var tLength = arrasgnd.length
     for(i=0; i<all.options.length; i++)
     {
          arrLookup[all.options[i].text] = all.options[i].value;
          if(all.options[i].selected && all.options[i].value != "" && all.options[i].value != "-1") 
          {
               arrasgnd[tLength] = all.options[i].text;
               tLength++;
          }
          else 
          {
               arrall[fLength] = all.options[i].text;
               fLength++;
          }
     }
     arrall.sort();
     arrasgnd.sort();
     all.length = 0;
     asgnd.length = 0;
     
     var c;
     for(c=0; c<arrall.length; c++) 
     {
          var no = new Option();
          no.value = arrLookup[arrall[c]];
          no.text = arrall[c];
          all[c] = no;
     }
     for(c=0; c<arrasgnd.length; c++) 
     {
     	var no = new Option();
     	no.value = arrLookup[arrasgnd[c]];
     	no.text = arrasgnd[c];
     	asgnd[c] = no;
     }

}
		
	function setAssignedVals(form_element_str)
	{
	    var assignedvals=""
	    form_element = eval(form_element_str);
	    //alert(form_element);
		for (var i=0;i < form_element.length;i++)
		{
  			assignedvals = assignedvals +","+ form_element.options[i].value;
  			//alert(form_element.options[i].value);
  		}
  		var tempstr = form_element_str+"_hidden";
  		//alert(tempstr);
  		form_element_hidden = eval(tempstr);
		form_element_hidden.value = assignedvals;
		//alert("Assigned vals Hidden==>"+form_element_hidden.value);
	}

     function doSubmit()
    {
        <%  
            for @appmoduleJs in @appmodules
                for @data_access_actionJs in @data_access_actions
        %>
    	setAssignedVals("document.data_access_matrix_form.vals_collectionlist_<%=@appmoduleJs.name%>_<%=@data_access_actionJs%>_selected");
    
        <%         end
            end
        %>	
    	document.data_access_matrix_form.submit();
    }

</script>


  <form name="data_access_matrix_form" action="/dataaccess/save" method="post" onsubmit="javascript:doSubmit();">
  <input type="hidden" name="role_id" value="<%=@role.id%>"/>
  
  <%
    @divDisplayStyle = "display: none;"
    @savedop = params[:saved_op]
    if (@savedop != nil and @savedop=='1')
        @divDisplayStyle = "display: block;"
    end
    
  %>
  <div  id="datasecuritylist" style="<%=@divDisplayStyle%>">
    <div  class="list">
      
      <h2>Data Security</h2>
 
        <table id="role_permission_table">
        <thead >
        <tr>
        <th>&nbsp;</th>
        <% for @data_access_action in @data_access_actions%>
        
        <input type="hidden" name="data_access_action[]" value="<%=@data_access_action%>"/>

        <th><%=@data_access_action.capitalize%></th>
        <%end%>
        </tr>
        </thead>
        
        <tbody>
      
          <%
            @data_access_flags = DataAccessFlag.find_all
            # Show only functional modules 
            for @appmodule in @appmodules
                @appmodule_all_records =  AjubyDataAuthorize.get_model_all_records(@appmodule.name)
          %>
          
        
        <tr>
            <td><%= @appmodule.name.capitalize %></td>
            <input type="hidden" name="module_name[]" value="<%=@appmodule.name%>"/>
            <% for @data_access_action in @data_access_actions%>
            
             <% @permission = get_permission(@appmodule.name, @data_access_action)
              if(@permission!=nil)
                @data_permission = get_data_permission(@permission.id, @role.id)
                @data_access_flag_str = "Allow"
                @data_access_flag_id_str = "1"
                if (@data_permission!=nil)
                    @data_access_flag_obj = DataAccessFlag.find(@data_permission.data_flag_id)
                    
                    if @data_access_flag_obj !=nil
                        @data_access_flag_str = @data_access_flag_obj.name
                        @data_access_flag_id_str = @data_access_flag_obj.id.to_s
                    end    
                end 
             %>
            
             <input type="hidden" name="action_<%=@appmodule.name%>_<%=@data_access_action%>" value="<%=@permission.id%>"/>
            <td>
            
                <select name="action_<%=@permission.id%>" onchange="javascript:checkCollection('action_<%=@permission.id%>', 'collectionlist_<%=@data_access_action%>_<%=@appmodule.name%>');">
                <%
                    for @data_access_flag in @data_access_flags
                    @selectedstring = ""
                    if (@data_access_flag.name == @data_access_flag_str)
                        
                        @selectedstring = "selected"
                    end
                %>
                    <option value="<%=@data_access_flag.id%>" <%=@selectedstring%> ><%=@data_access_flag.name%></option>
                <%end%>     
                
                 </select>
                  <br/>
                       <%= render :partial => "dataaccess/collection" , 
                                  :locals => { :type => @data_access_action, 
                                               :model_name=> @appmodule.name,
                                               :appmodule_all_records=> @appmodule_all_records,
                                               :role_id=> @role.id,
                                               :daf_id=> @data_access_flag_id_str}%> 
  
                 
            </td>
               <%end%>
            <%end%>
           
        </tr>
        <tr>
        <td colspan="5">
            
        </td>
        </tr>
        
        <%end%>
        <tbody>
        
        
        
      </table>
     <!-- Data security code ends here -->
      
    <br/>
           
       <div class="list">
    	<table>
    	     <th>Actions</th>
             <tr class="even">
                <td>
                    <!-- %= submit_tag 'Save' % -->
                    <%= image_submit_tag "/themes/odotclassic/images/btn_save.png", :alt => 'Save', :border => 0 %>
                    <%= link_if_authorized image_tag('/themes/odotclassic/images/btn_showrole.png', :alt => 'Show'), {:action => 'show', :id => @role}  %>
                    <%= link_if_authorized image_tag('/themes/odotclassic/images/btn_listroles.png', :alt => 'List'), {:action => 'list'}  %>  
                </td>
             </tr>        
    	</table>
    </div>
    
   </div>
</div>

   </form>
